package com.ohuang.util_u;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * ListView适配器
 */
public abstract class SimpleBaseAdapter extends BaseAdapter {


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View cpt = null;
        ViewHolder viewHolder = null;
        if (convertView != null) {
            cpt = convertView;
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (cpt == null) {
            viewHolder = onCreateViewHolder(parent, getItemViewType(position), position);
            cpt = viewHolder.itemView;
            cpt.setTag(viewHolder);
        }
        viewHolder.viewType=getItemViewType(position);
        viewHolder.position=position;

        onBindViewHolder(viewHolder,position, parent);
        return cpt;
    }


    public static class ViewHolder {
        View itemView;
        int position;
        int viewType;

        public ViewHolder(View itemView) {
            if (itemView == null) {
                throw new IllegalArgumentException("can not null itemView");
            }
            this.itemView = itemView;
        }

        public View getItemView() {
            return itemView;
        }

        public int getPosition() {
            return position;
        }

        public int getViewType() {
            return viewType;
        }
    }

    public abstract ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, int position);


    public abstract void onBindViewHolder(ViewHolder holder, int position, ViewGroup parent);
}
